package com.atguigu.day20.homework.h3;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/25 20:30
 * @注释
 */
public class Account {
    private final String id;
    private double balance;

    public String getId() {
        return id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "卡号: '" + id + '\'' +
                ", 余额: " + balance
                ;
    }

    public Account(String id, double balance) {
        this.id = id;
        this.balance = balance;
    }

    public synchronized void save(double money) {
        if (money > 0){
            balance += money;
            System.out.println("丈夫" + Thread.currentThread().getName() + "本次存钱" + money + this);
        }
        this.notify();
    }
    public synchronized void withdraw(double money) {
        if (money > 0 && balance >= money){
            balance -= money;
            System.out.println("妻子" + Thread.currentThread().getName() + "准备取" + money + "元," + this);
        }
        if (money > balance){
            System.out.println("妻子" + Thread.currentThread().getName() +"本次想取钱" + money +"元，余额不足，等待...");
        }
        try {
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (money < 0){
            System.out.println("妻子" + Thread.currentThread().getName() +"本次想取钱" + money +"格式错误...");
        }
        this.notify();
    }
}
